using System;
using System.Collections.Generic;
using System.Data;

using PMServiceDesk.DataType;
using PMServiceDesk.Utils.ManejoDatos;
using PMServiceDesk.Parameters.Constantes;

namespace PMServiceDesk.DataAccess
{
    public class DiaHorarioData
    {
        #region Variables

        private string _select;
        private string _join;
        private string _where;
        private int _totalRegistros = 0;

        private Dictionary<string, object> _parametrosConsulta;

        #endregion

        #region Propiedades

        protected string Where
        {
            get
            {
                if (_where == "") return "";

                return "WHERE " + _where.Substring(3);
            }
        }

        #endregion

        #region Constructores

        public DiaHorarioData()
        {
            this._limpiarConsulta();
        }

        #endregion

        #region Metodos

        private void _limpiarConsulta()
        {
            _select = "";
            _join = "";
            _where = "";
            _parametrosConsulta = new Dictionary<string, object>();
        }

        private DataUtil _consultaDataReader(int paginaActual = 0, int cantidadMostrar = 0, string orderBy = "")
        {
            DataUtil data = null;

            if(paginaActual==0)
                data = new DataUtil(TypeDataBase.PMServiceDesk, CommandType.Text, _select + _join + this.Where + ((orderBy == "") ? "" : " ORDER BY " + orderBy), _parametrosConsulta);
            else
                data = DataReader.ReturnPagingReader(TypeDataBase.PMServiceDesk, _select, _join, this.Where, this._parametrosConsulta, paginaActual, cantidadMostrar, orderBy, ref _totalRegistros);

            this._limpiarConsulta();

            return data;
        }

        public void Grabar(DiaHorarioType diaHorario)
        {
            using (DataUtil d = this._grabar())
            {
                d.AddParameter("@ID", (diaHorario.Id == null ? "0" : diaHorario.Id));
                d.AddParameter("@DIACALENDARIOID", diaHorario.DiaCalendarioId);
                d.AddParameter("@HORARIOINICIO", ConvertFormat.Char_To_Date(DefaultValues.Fecha_Default + " " + diaHorario.HorarioInicio,Formatos.Fecha_DDMMYYYY_HHmm) );
                d.AddParameter("@HORARIOFIN", ConvertFormat.Char_To_Date(DefaultValues.Fecha_Default + " " + diaHorario.HorarioFin, Formatos.Fecha_DDMMYYYY_HHmm));

                diaHorario.Id = d.ExecuteScalar(true).ToString();
            }
        }

        public void Eliminar(string id)
        {
            using (DataUtil d = this._eliminar())
            {
                d.AddParameter("@ID", id);

                d.ExecuteNonQuery();
            }
        }


        public DiaHorarioType Cargar()
        {
            List<DiaHorarioType> l = this.CargarLista();

            if (l.Count == 0) return null;

            return l[0];
        }

        public List<DiaHorarioType> CargarLista()
        {
            List<DiaHorarioType> l = new List<DiaHorarioType>();

            using (DataUtil d = this._consultaCargar())
                using (IDataReader r = d.ExecuteReader())
                {
                    DiaHorarioType o = null;

                    while (r.Read())
                    {
                        o = new DiaHorarioType();

                        o.Id = r.GetValue(d.GetOrdinalReader("ID")).ToString();
                        o.DiaCalendarioId = r.GetValue(d.GetOrdinalReader("DIACALENDARIOID")).ToString();
                        o.HorarioInicio = ConvertFormat.Date_To_Char(r.GetValue(d.GetOrdinalReader("HORARIOINICIO")), "HH:mm");
                        o.HorarioFin = ConvertFormat.Date_To_Char(r.GetValue(d.GetOrdinalReader("HORARIOFIN")), "HH:mm"); 
                        
                        l.Add(o);
                    }
                }

            return l;
        }

        public List<DiaHorarioType> CargarListaVista(int paginaActual = 0, int cantidadMostrar = 0, string orderBy = "")
        {
            List<DiaHorarioType> l = new List<DiaHorarioType>();

            using (DataUtil d = this._consultaCargarVista(paginaActual, cantidadMostrar,orderBy))
                using (IDataReader r = d.ExecuteReader())
                {
                    DiaHorarioType o = null;

                    while (r.Read())
                    {
                        o = new DiaHorarioType();

                        o.Id = r.GetValue(d.GetOrdinalReader("ID")).ToString();
                        o.DiaCalendarioId = r.GetValue(d.GetOrdinalReader("DIACALENDARIOID")).ToString();
                        o.HorarioInicio = ConvertFormat.Date_To_Char(r.GetValue(d.GetOrdinalReader("HORARIOINICIO")),"HH:mm");
                        o.HorarioFin = ConvertFormat.Date_To_Char(r.GetValue(d.GetOrdinalReader("HORARIOFIN")), "HH:mm");

                        l.Add(o);
                    }
                }

            if (l.Count <= _totalRegistros) l.Capacity = _totalRegistros;

            return l;
        }

        #endregion

        #region SQL

        private DataUtil _eliminar()
        {
            string sql = @"DELETE FROM DIAHORARIO WHERE ID=@ID";

            return new DataUtil(TypeDataBase.PMServiceDesk, CommandType.Text, sql);
        }

        private DataUtil _grabar()
        {
            string sql = @"IF(NOT EXISTS(SELECT 1 FROM DIAHORARIO WHERE ID=@ID)) 
                           BEGIN
                               INSERT INTO DIAHORARIO
                               (
                                   DIACALENDARIOID
                                  ,HORARIOINICIO
                                  ,HORARIOFIN
                               )
                               VALUES
                               (
                                   @DIACALENDARIOID
                                  ,@HORARIOINICIO
                                  ,@HORARIOFIN
                               )

                               SELECT @@IDENTITY
                           END
                           ELSE
                           BEGIN
                               UPDATE DIAHORARIO SET
                               DIACALENDARIOID=@DIACALENDARIOID
                              ,HORARIOINICIO=@HORARIOINICIO
                              ,HORARIOFIN=@HORARIOFIN
                               WHERE ID=@ID

                               SELECT @ID
                           END";

            return new DataUtil(TypeDataBase.PMServiceDesk, CommandType.Text, sql);
        }

        private DataUtil _consultaCargar()
        {
            _select = @"SELECT
                        DI.ID
                       ,DI.DIACALENDARIOID
                       ,DI.HORARIOINICIO
                       ,DI.HORARIOFIN
                        ";

            _join = @"FROM DBO.DIAHORARIO [DI] 
                      ";

            return this._consultaDataReader();
        }

        private DataUtil _consultaCargarVista(int paginaActual = 0, int cantidadMostrar = 0, string orderBy = "")
        {
            _select = @"SELECT
                        DI.ID
                       ,DI.DIACALENDARIOID
                       ,DI.HORARIOINICIO
                       ,DI.HORARIOFIN
                        ";

            _join = @"FROM DBO.DIAHORARIO [DI] 
                      ";

            return this._consultaDataReader(paginaActual, cantidadMostrar, orderBy);
        }

        public void FiltrarPorId(string id)
        {
            _where += @"AND DI.ID=@ID
                                 ";

            _parametrosConsulta.Add("@ID", id);
        }
        public void FiltrarPorDiaCalendarioId(string diaCalendarioId)
        {
            _where += @"AND DI.DIACALENDARIOID=@DIACALENDARIOID
                                 ";

            _parametrosConsulta.Add("@DIACALENDARIOID", diaCalendarioId);
        }

        #endregion
    }
}
